﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Collections;
using Dictionary;

namespace Dictionary.PresentationLayer
{
    /// <summary>
    /// Interaction logic for Word.xaml
    /// </summary>
    public partial class Word : Window
    {
        string mode, fn, word;
        string ok;

        public Word(string m, string fina)
        {
            mode = m;
            fn = fina;
            InitializeComponent();
            Init();
        }

        public Word(string m, string fina, string w)
        {
            mode = m;
            fn = fina;
            InitializeComponent();
            Init(w);
        }
#region Function
        //Ham khoi tao cho che do them tu
        private void Init()
        {
            lblTitle.Content = "Thêm Từ Vựng Mới";
            txtEnglish.Text = "";
            txtPhonetic.Text = "";
            txtVietnamese.Text = "";
            txtImage.Text = "";
            ok = null;
        }

        //Ham khoi tao cho che do them tu
        private void Init(string w)
        {
            lblTitle.Content = "Sửa Từ Vựng";
            ok = null;
            ArrayList arr = BusLayer.LibsContent.LoadContent(fn, w);
            word = w;
            if (arr != null)
            {
                txtEnglish.Text = arr[0].ToString();
                txtPhonetic.Text = arr[1].ToString();
                txtVietnamese.Text = arr[2].ToString();
            }
            txtImage.Text="";
            //Check image
            if (BusLayer.LibsContent.CheckExistImage(System.IO.Path.GetFileNameWithoutExtension(fn),word + ".jpg") == true)
            {
                txtImage.Text = BusLayer.LibsContent.GetDirImage() + "\\" + System.IO.Path.GetFileNameWithoutExtension(fn) + "\\" + word + ".jpg";
            }
            else
                if (BusLayer.LibsContent.CheckExistImage(System.IO.Path.GetFileNameWithoutExtension(fn),word + ".bmp") == true)
                {
                    txtImage.Text = BusLayer.LibsContent.GetDirImage() + "\\" + System.IO.Path.GetFileNameWithoutExtension(fn) + "\\" + word + ".bmp";
                }
                else
                    if (BusLayer.LibsContent.CheckExistImage(System.IO.Path.GetFileNameWithoutExtension(fn),word + ".png") == true)
                    {
                        txtImage.Text = BusLayer.LibsContent.GetDirImage() + "\\" + System.IO.Path.GetFileNameWithoutExtension(fn) + "\\" + word + ".png";
                    }
            //end
        }

        //ham goi effect cua cac nut min, max, close
        private void ControlButton(System.Windows.Controls.Image i, string s)
        {
            i.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
            Storyboard stb = new Storyboard();
            stb = (Storyboard)Resources[s];
            i.BeginStoryboard(stb);
        }

        private void ScaleControl(TextBox ctr)
        {
            Storyboard stb = new Storyboard();
            stb = (Storyboard)Resources["ScaleControl"];
            ctr.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
            ctr.BeginStoryboard(stb);
        }

        private void ScaleControl(Button ctr)
        {
            Storyboard stb = new Storyboard();
            stb = (Storyboard)Resources["ScaleControl"];
            ctr.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
            ctr.BeginStoryboard(stb);
        }

        public string GetOk()
        {
            return ok;
        }

        private bool CheckValid()
        {
            if (txtEnglish.Text == "")
            {
                txtEnglish.Focus();
                MessageBox.Show("Bạn phải nhập từ vựng cần dịch nghĩa vào !", "Thông Báo");
                return false;
            }
            if (txtPhonetic.Text == "")
            {
                txtPhonetic.Focus();
                MessageBox.Show("Bạn hãy nhập phiên âm cho từ vựng !", "Thông Báo");
                return false;
            }
            if (txtVietnamese.Text == "")
            {
                txtVietnamese.Focus();
                MessageBox.Show("Bạn phải nhập nghĩa cho từ vựng!", "Thông Báo");
                return false;
            }
            return true;
        }
#endregion

        private void imgClose_MouseEnter(object sender, MouseEventArgs e)
        {
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri("../Image/Close_2.png", UriKind.Relative);
            bi.EndInit();
            ImageSource img = bi;
            imgClose.Source = img;
            ControlButton(imgClose, "ControlButton_Down");
        }

        private void imgClose_MouseLeave(object sender, MouseEventArgs e)
        {
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri("../Image/Close.png", UriKind.Relative);
            bi.EndInit();
            ImageSource img = bi;
            imgClose.Source = img;
            ControlButton(imgClose, "ControlButton_Up");
        }

        private void imgClose_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void lblTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void txtEnglish_MouseEnter(object sender, MouseEventArgs e)
        {
            ScaleControl(txtEnglish);
        }

        private void txtPhonetic_MouseEnter(object sender, MouseEventArgs e)
        {
            ScaleControl(txtPhonetic);
        }

        private void txtVietnamese_MouseEnter(object sender, MouseEventArgs e)
        {
            ScaleControl(txtVietnamese);
        }

        private void bttOk_Click(object sender, RoutedEventArgs e)
        {
            if (CheckValid() == false)
                return;

            if (mode == "ADD")
            {
                if (BusLayer.ModifyBus.AddWord(fn,txtEnglish.Text,txtPhonetic.Text,txtVietnamese.Text)==true)
                {
                    ok = txtEnglish.Text;
                    if (txtImage.Text != "")
                    {
                        if (BusLayer.ModifyBus.CopyImage(System.IO.Path.GetFileNameWithoutExtension(fn),txtImage.Text, ok) == false)
                        {
                            System.Windows.Forms.MessageBox.Show("Chép file hình ảnh minh họa không thành công !", "Thêm Từ Vựng Mới");
                        }
                    }
                    Close();
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Từ này đã tồn tại rồi !", "Thêm Từ Vựng Mới");
                }
            }
            else
            {
                if (BusLayer.ModifyBus.ModifyWord(fn, txtEnglish.Text, txtPhonetic.Text, txtVietnamese.Text,word) == true)
                {
                    ok = txtEnglish.Text;
                    if (txtImage.Text != "")
                    {
                        if (BusLayer.ModifyBus.CopyModifyImage(System.IO.Path.GetFileNameWithoutExtension(fn), txtImage.Text, word, ok) == false)
                        {
                            System.Windows.Forms.MessageBox.Show("Chép file hình ảnh minh họa không thành công !", "Sửa Từ Vựng");
                        }
                    }
                    else
                    {
                        if (BusLayer.LibsContent.CheckExistImage(System.IO.Path.GetFileNameWithoutExtension(fn), word + ".jpg") == true)
                        {
                            DataLayer.FileDir.DeleteFile(BusLayer.LibsContent.GetDirImage() + "\\" + System.IO.Path.GetFileNameWithoutExtension(fn) + "\\" + word + ".jpg");
                        }
                        else
                            if (BusLayer.LibsContent.CheckExistImage(System.IO.Path.GetFileNameWithoutExtension(fn), word + ".bmp") == true)
                            {
                                DataLayer.FileDir.DeleteFile(BusLayer.LibsContent.GetDirImage() + "\\" + System.IO.Path.GetFileNameWithoutExtension(fn) + "\\" + word + ".bmp");
                            }
                            else
                                if (BusLayer.LibsContent.CheckExistImage(System.IO.Path.GetFileNameWithoutExtension(fn), word + ".png") == true)
                                {
                                    DataLayer.FileDir.DeleteFile(BusLayer.LibsContent.GetDirImage() + "\\" + System.IO.Path.GetFileNameWithoutExtension(fn) + "\\" + word + ".png");
                                }
                    }
                    Close();
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Từ này đã tồn tại rồi !", "Sửa Từ Vựng");
                }
            }
        }

        private void bttCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void txtImage_MouseEnter(object sender, MouseEventArgs e)
        {
            ScaleControl(txtImage);
        }

        private void bttImage_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openDialog = new Microsoft.Win32.OpenFileDialog();
            openDialog.DefaultExt = ".jpg";
            openDialog.Filter = "All Image Format Support (*.jpg, *.bmp, *.png)|*.jpg;*.bmp;*.png";
            if (openDialog.ShowDialog().Value == true)
            {
                txtImage.Text = openDialog.FileName;
            }
        }
    }
}
